package com.ailanyin.updatesql.config;

import com.ailanyin.updatesql.service.UpdateSqlService;
import com.ailanyin.updatesql.service.impl.UpdateSqlServiceImpl;
import com.ailanyin.updatesql.utils.DbUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;

/**
 * 获取yml配置信息
 *
 * @author ailanyin
 */
@ConfigurationProperties(prefix = "ailanyin.updatesql")
public class BaseConfig {

    /**
     * 开关  默认关闭
     */
    private boolean needRun = false;
    /**
     * sql文件的父级目录
     */
    private String filepath = "sql";
    /**
     * 数据库 URL
     */
    private String databaseUrl;
    /**
     * 数据库账号
     */
    private String databaseUsername;
    /**
     * 数据库密码
     */
    private String databasePassword;
    /**
     * 数据库驱动  默认 mysql
     */
    private String databaseDriver = "com.mysql.cj.jdbc.Driver";


    @PostConstruct
    private void init() {
        if (needRun) {
            DbUtil.startConnection(databaseDriver,databaseUrl,databaseUsername,databasePassword);
            UpdateSqlService updateSqlService = new UpdateSqlServiceImpl();
            updateSqlService.startUpdate(filepath);
            DbUtil.closeConnection();
        }
    }

    public boolean isNeedRun() {
        return needRun;
    }

    public void setNeedRun(boolean needRun) {
        this.needRun = needRun;
    }

    public String getFilepath() {
        return filepath;
    }

    public void setFilepath(String filepath) {
        this.filepath = filepath;
    }

    public String getDatabaseUrl() {
        return databaseUrl;
    }

    public void setDatabaseUrl(String databaseUrl) {
        this.databaseUrl = databaseUrl;
    }

    public String getDatabaseUsername() {
        return databaseUsername;
    }

    public void setDatabaseUsername(String databaseUsername) {
        this.databaseUsername = databaseUsername;
    }

    public String getDatabasePassword() {
        return databasePassword;
    }

    public void setDatabasePassword(String databasePassword) {
        this.databasePassword = databasePassword;
    }

    public String getDatabaseDriver() {
        return databaseDriver;
    }

    public void setDatabaseDriver(String databaseDriver) {
        this.databaseDriver = databaseDriver;
    }
}
